<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="订单号" v-model="listQuery.orderSn"> </el-input>
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="商品名称" v-model="listQuery.goodsName"> </el-input>
    <el-select @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="评价星级" v-model="listQuery.refundWay"> 
      <el-option v-for="item in  starNumOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
    </el-select>
    <el-date-picker class="filter-item" v-model="dateRange" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" placeholder="评价时间">
    </el-date-picker>
    <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
  </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
    <el-table-column align="center" label="订单号" min-width="10%"> <template scope="scope">
          <span>{{scope.row.orderSn}}</span>
        </template> </el-table-column>
    <el-table-column min-width="10%" align="center" label="商品名称"> <template scope="scope">
        <span>{{scope.row.goodsName}}</span>
      </template> </el-table-column>
    <el-table-column min-width="10%" align="center" label="店铺名称"> <template scope="scope">
        <span>{{scope.row.shopName}}</span>
      </template> </el-table-column>
    <el-table-column min-width="10%" align="center" label="星级"> <template scope="scope">
      <el-rate v-model="scope.row.starNum"></el-rate>
          </template> </el-table-column>
    <el-table-column min-width="20%" align="center" label="评价内容"> <template scope="scope">
            <span>{{scope.row.commentContent}}</span>
          </template> </el-table-column>
    <el-table-column min-width="10%" align="center" label="评价时间"> <template scope="scope">
            <span>{{scope.row.commentTime}}</span>
          </template> </el-table-column>
    <el-table-column min-width="10%" align="center" label="买家"> <template scope="scope">
            <span>{{scope.row.userNickName}}</span>
          </template> </el-table-column>
    <el-table-column min-width="10%" align="center" label="回复内容"> <template scope="scope">
            <span>{{scope.row.replyContent}}</span>
          </template> </el-table-column>
    <el-table-column align="center" label="操作" min-width="20%"> <template scope="scope">
        <el-button v-if="commentManager_btn_reply" size="small" type="success" @click="handleReply(scope.row)">回复
        </el-button>
        <el-button v-if="commentManager_btn_del" size="small" type="danger" @click="handleDel(scope.row)">删除
        </el-button>
      </template> </el-table-column>
  </el-table>

  <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="20%">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item prop="replyContent" label="回复内容"  placeholder="请输入回复内容">
        <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 5}" placeholder="请输入回复内容" v-model="form.replyContent"> </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button type="primary" @click="update('form')">回复</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {
  page,
  putObj,
  delObj
} from 'api/shop/order/comment/index';
import { mapGetters } from 'vuex';
export default {
  name: 'comment',
  data() {
    return {
      form:{
        commentId:undefined,
        replyContent:undefined
      },
      rules: {
        replyContent: [
          {
            required: true,
            message: '请输入回复内容',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 200,
            message: '长度在 1 到 200 个字符',
            trigger: 'blur'
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      dateRange : null,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        orderSn: undefined,
        goodsName:undefined,
        startTime : undefined,
        endTime : undefined,
        starNum:undefined
      },
      commentManager_btn_reply: false,
      commentManager_btn_del: false,
      starNumOptions:[
        {value:"1",label: '1星'},
        {value:"2",label: '2星'},
        {value:"3",label: '3星'},
        {value:"4",label: '4星'},
        {value:"5",label: '5星'}
      ],
      dialogFormVisible: false,
      dialogTitle:'回复',
      tableKey: 0
    }
  },
  created() {
    this.getList();
    this.commentManager_btn_reply = this.elements['commentManager:btn_reply'];
    this.commentManager_btn_del = this.elements['commentManager:btn_del'];
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    getList() {
      this.listLoading = true;
      page(this.listQuery)
        .then(response => {
          this.list = response.data.rows;
          this.total = response.data.total;
          this.listLoading = false;
        })
    },
    handleFilter() {
      if(this.dateRange != null){
        this.listQuery.startTime = this.dateRange[0];
        this.listQuery.endTime = this.dateRange[1];
      }
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val;
      this.getList();
    },
    handleReply(row) {
      this.resetFormTemp();
      this.form.commentId = row.commentId;
      this.dialogFormVisible = true;
    },
    handleDel(row){
      this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          delObj(row.commentId).then(() => {
              this.getList();
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
            });
        }).catch(() => {
        });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          putObj(this.form.commentId, this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    resetFormTemp() {
      this.form = {
        commentId:undefined,
        deleteFlag:undefined,
        deleteTime:undefined,
        replyContent:undefined
      };
    }
  }
}
</script>
<style scoped>

</style>